package com.stbu;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.stbu.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class JsonController {

    @ResponseBody
    @RequestMapping("/testJson")
    public String jsonTest() throws JsonProcessingException {
        List<User> list = new ArrayList<>();

        User user = new User();
        user.setId(33);
        user.setUsername("202303");
        user.setPassword("030303");
        ObjectMapper json = new ObjectMapper();

        //数组结构
        User user1 = new User();
        user1.setId(333);
        user1.setUsername("admin333");
        user1.setPassword("pwd333");

        list.add(user);
        list.add(user1);
        return json.writeValueAsString(list);
    }
}
